import 'dart:collection';

import 'package:changxindianzishangwu/dao/common_language_dao.dart';
import 'package:changxindianzishangwu/util/app_color.dart';
import 'package:changxindianzishangwu/util/toast.dart';
import 'package:changxindianzishangwu/widget/app_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

///添加打招呼内容1 自动回复2 常用语3[另外的接口]

class CommonLanguageAddPage extends StatefulWidget {
  String context = "";
  String id = "";
  int type;

  CommonLanguageAddPage(this.context, this.id, this.type, {Key key})
      : super(key: key);

  @override
  _CommonLanguageAddPageState createState() => _CommonLanguageAddPageState();
}

class _CommonLanguageAddPageState extends State<CommonLanguageAddPage> {
  TextEditingController _controller;

  @override
  void initState() {
    // TODO: implement initState
    _controller = TextEditingController(text: widget.context);
    super.initState();
  }

  //正在请求参数
  bool _isPost = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TopAppBar(
        context,
        widget.context.isEmpty ? "添加" : "编辑",
        rightText: widget.context.isEmpty ? "" : "删除",
        rightCallback: () {
          if (widget.context.isEmpty) return;
          _delete();
        },
      ),
      body: Column(
        children: [
          Container(
            margin: EdgeInsets.all(10),
            padding: EdgeInsets.all(10),
            color: Colors.white,
            child: TextField(
              cursorColor: AppColor.colorTheme,
              maxLength: 200,
              controller: _controller,
              maxLines: 5,
              keyboardType: TextInputType.text,
              style: TextStyle(color: AppColor.colorText85, fontSize: 16),
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.zero, border: InputBorder.none),
            ),
          ),
          TextButton(
              onPressed: () {
                if (_controller.text.isEmpty) {
                  toast("请输入内容");
                  return;
                }
                if (_isPost) return;
                _isPost = true;
                widget.context.isEmpty ? _addCommon() : _update();
              },
              child: Container(
                alignment: Alignment.center,
                margin: EdgeInsets.only(top: 15, bottom: 15, right: 15),
                height: ScreenUtil().setHeight(44),
                width: ScreenUtil().setWidth(150),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(6),
                  color: AppColor.colorTheme,
                ),
                child: Text(
                  widget.context.isEmpty ? "确认添加" : "完成",
                  style: TextStyle(
                      color: Colors.white, fontSize: ScreenUtil().setSp(15)),
                ),
              ))
        ],
      ),
    );
  }

  ///添加自动回复
  void _addCommon() async {
    var hashMap =
        await CommonLanguageDao.saveData(widget.type, _controller.text);
    if (hashMap["resultCode"] == 1) {
      Get.back(result: "ok");
    } else {
      toast(hashMap["resultMsg"]);
    }
  }

  ///删除自动回复
  void _delete() async {
    var hashMap = await CommonLanguageDao.deleteData(widget.type, widget.id);
    if (hashMap["resultCode"] == 1) {
      Get.back(result: "ok");
    } else {
      toast(hashMap["resultMsg"]);
    }
  }

  ///修改自动回复
  void _update() async {
    var hashMap = await CommonLanguageDao.updateData(
        widget.type, widget.id, _controller.text);
    if (hashMap["resultCode"] == 1) {
      Get.back(result: "ok");
    } else {
      toast(hashMap["resultMsg"]);
    }
  }
}
